package com.css.fxfzypg.modules.auxiliaryDecisionManage.auxiliaryDecision.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.logInfo.SysLog;
import com.css.fxfzypg.modules.auxiliaryDecisionManage.auxiliaryDecision.entity.AuxiliaryDecision;
import com.css.fxfzypg.modules.auxiliaryDecisionManage.auxiliaryDecision.param.AuxiliaryDecisionParam;
import com.css.fxfzypg.modules.auxiliaryDecisionManage.auxiliaryDecision.service.AuxiliaryDecisionService;
import com.css.fxfzypg.modules.downloadLegend.service.SupergraphService;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.util.ResponseCreator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/03/23
 * @Description: 震后辅助决策管理
 */
@RestController
@RequestMapping("/auxiliaryDecision")
@Slf4j
public class AuxiliaryDecisionController {
    @Resource
    private AuxiliaryDecisionService auxiliaryDecisionService;
    @Resource
    private SupergraphService supergraphService;

    /***
     * 获取最新地震信息,并将不重复的信息写入 , 每次点击页面的时候调用
     * @return
     */
    @PostMapping("/save")
    public RestResponse writeData(){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            // auxiliaryDecisionService.writeData();
            jsonObject.put("message","写入成功");
            restResponse= RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "写入失败";
            }
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 分页列表
     * @param param 参数
     * @return page
     */
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request, AuxiliaryDecisionParam param) {
        RestResponse restResponse = null;
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = auxiliaryDecisionService.getPage(curPage, pageSize,param);
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 灾害情景分析修改
     * @param param 参数
     * @return
     */
    @PostMapping("/updateErcmEntityById")
    public RestResponse updateErcmEntityById(@RequestBody AuxiliaryDecision param) {
        try {
            auxiliaryDecisionService.updateErcmEntityById(param);
            return RestResponse.succeed("修改成功");
        } catch (Exception e) {
            log.error("修改数据失败!", e);
            return RestResponse.fail("修改数据失败!");
        }
    }
    /**
     * 应急处置对策修改
     * @param param 参数
     * @return
     */
    @PostMapping("/updateDisasterSceneById")
    public RestResponse updateDisasterSceneById(@RequestBody AuxiliaryDecision param) {
        try {
            auxiliaryDecisionService.updateDisasterSceneById(param);
            return RestResponse.succeed("修改成功");
        } catch (Exception e) {
            log.error("修改数据失败!", e);
            return RestResponse.fail("修改数据失败!");
        }
    }

    /**
     * 根据id查询
     * @param id 主键id
     * @return
     */
    @GetMapping("/findById")
    @Deprecated
    public RestResponse findById(String id) {
        try {
            AuxiliaryDecision data = auxiliaryDecisionService.findById(id);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 生成报告
     * @param map
     * @return
     */
    @SysLog("生成辅助决策报告")
    @PostMapping("/createReport")
    public RestResponse createReport(@RequestBody Map<String,Object> map) throws Exception {
        if(PlatformObjectUtils.isEmpty(map.get("id"))){
            return ResponseCreator.createFailResponse("请选择要生成报告的辅助决策数据");
        }
        if(PlatformObjectUtils.isEmpty(map.get("attachId"))){
            return ResponseCreator.createFailResponse("请选择要生成报告的报告模板");
        }
        //震后辅助决策分析结果打印出图
        RestResponse restResponse = supergraphService.realAnalysePrintImage((String) map.get("id"));
        Map<String,Object> resultMap = auxiliaryDecisionService.createReport((String) map.get("id"),(String) map.get("attachId"));
        return ResponseCreator.createSuccessResponse(resultMap,"生成成功");
    }

    /***
     * 报告下载
     */
    @PostMapping("/downloadReport")
    public void downloadReport(@RequestParam("reportId")String reportId, HttpServletResponse res){
        auxiliaryDecisionService.downloadReport(reportId,res);
    }


    /***
     * 报告管理获取模板信息
     */
    @GetMapping("/getReport")
    public RestResponse getReport(String id){
        Map<String,Object> map = auxiliaryDecisionService.getReport(id);
        return ResponseCreator.createSuccessResponse(map);
    }

    /***
     * 获取灾害场景信息 findById废弃 后续可删除
     */
    @GetMapping("/getDisasterScenario")
    public RestResponse getDisasterScenario(@RequestParam String id){
        return ResponseCreator.createSuccessResponse(auxiliaryDecisionService.getDisasterScenario(id));
    }

    /***
     * 获取应急处置对策信息 findById废弃 后续可删除
     */
    @GetMapping("/getEmergencyResponse")
    public RestResponse getEmergencyResponse(@RequestParam String id){
        return ResponseCreator.createSuccessResponse(auxiliaryDecisionService.getEmergencyResponse(id));
    }


}
